/**
 * This program is free software but WITHOUT ANY WARRANTY OR CONDITIONS OF ANY KIND.
 *
 * Copyright (C) 2008-2013 www.pretty-tools.com
 */

yrprxObserverServiceHelper.sendToContentScript = function (subject, topic, someData)
{
    try
    {
        chrome.tabs.query({'status': 'complete', 'url': '*://*/*'}, function(tabs) {
            for (var i in tabs)
            {
                try
                {
                    chrome.tabs.sendMessage(tabs[i].id, {call: "yrprxObserverServiceHelper.notifyLocalObservers", args: [subject, topic, someData]});
                }
                catch(e)
                {
                    if (yrprxObserverServiceHelper.logger.enabled) yrprxObserverServiceHelper.logger.error("sendToContentScript: ", ex);
                }
            }
        });
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("sendToContentScript: ", ex);
    }
}

// subscribe to events
chrome.runtime.onConnect.addListener(function(port) {
    if (port.name == "ObserverService")
    {
        port.onMessage.addListener(function(msg) {
            yrprxObserverServiceHelper.notifyLocalObservers(msg.subject, msg.topic, msg.data);

            if (yrprxObserverServiceHelper.forwardEventsToContentScript)
                yrprxObserverServiceHelper.sendToContentScript(msg.subject, msg.topic, msg.data);
        });
    }
});
